package com.pianzhu.blockchainemr.service.impl;

import com.pianzhu.blockchainemr.mapper.PatientMapper;
import com.pianzhu.blockchainemr.pojo.*;
import com.pianzhu.blockchainemr.pojo.Param.EmpowerParam;
import com.pianzhu.blockchainemr.pojo.result.*;
import com.pianzhu.blockchainemr.service.PatientService;
import com.pianzhu.blockchainemr.utils.CurrentHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;

    @Override
    public List<EmrHospital> getHospitalList() {
        return patientMapper.getHospitalList();
    }

    @Override
    public List<EmrDeptDict> getDeptList() {
        return patientMapper.getDeptList();
    }

    @Override
    public List<EmrUser> getDoctorList(String deptId, Integer hospitalId) {
        return patientMapper.getDoctorList(deptId, hospitalId);
    }

    @Override
    @Transactional
    public void appointment(EmrMedVisit visit) {
        LocalDateTime startOfDay = LocalDate.now().atStartOfDay();
        LocalDateTime endOfDay = LocalDate.now().atTime(23, 59, 59);

        // 转换为 Date 类型
        Date startDate = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date endDate = Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
        //获取今日加入的记录数，记录数+1 -》序列号
        int count = patientMapper.countTodayRecordsByDateRange(startDate, endDate);
        count++;
        String today = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
        String visitId = "V"+today+String.format("%04d", count);
        visit.setVisitId(visitId);
        visit.setVisitType("01");
        visit.setVisitStatus("1");
        visit.setCreatedTime(LocalDateTime.now());
        visit.setPatientId(patientMapper.selectPatientIdByUserId(CurrentHolder.getCurrentLocal().getId()));

        visit.setAdmissionTime(LocalDateTime.now());
        visit.setCreatedTime(LocalDateTime.now());
        visit.setCreatorId(Long.valueOf(CurrentHolder.getCurrentLocal().getId()));

        patientMapper.appointment(visit);
    }

    @Override
    @Transactional
    public List<MedicalRecordListResult> getMedicalRecordList(String username) {
        String patientId = patientMapper.selectPatientIdByUsername(username);
        List<MedicalRecordListResult> resultList = patientMapper.getMedicalRecordList(patientId);
        for (MedicalRecordListResult medicalRecordListResult : resultList) {
            switch (medicalRecordListResult.getDocType()){
                case "01":
                    medicalRecordListResult.setDocType("入院记录");
                    break;
                case "02":
                    medicalRecordListResult.setDocType("病程记录");
                    break;
                case "03":
                    medicalRecordListResult.setDocType("出院小结");
                    break;
                case "04":
                    medicalRecordListResult.setDocType("门诊病历");
                    break;
                default:
                    medicalRecordListResult.setDocType("其他");
            }
            MedicalRecordInfo medicalRecordInfo = patientMapper.getMedicalRecordInfo(medicalRecordListResult.getDocId());
            medicalRecordInfo.setPatientName(patientMapper.selectRealNameByPatientId(medicalRecordInfo.getPatientId()));
            medicalRecordInfo.setDoctorName(patientMapper.selectRealNameById(medicalRecordInfo.getDoctorId()));
            medicalRecordInfo.setDeptName(patientMapper.selectDeptNameByDeptCode(medicalRecordInfo.getDeptCode()));

            medicalRecordListResult.setMedicalRecordInfo(medicalRecordInfo);
        }
        return resultList;
    }

    @Override
    public List<MedicalReportListResult> getReportList(String username) {
        String patientId = patientMapper.selectPatientIdByUsername(username);
        List<MedicalReportListResult> reportList = patientMapper.getReportList(patientId);
        for (MedicalReportListResult medicalReportListResult : reportList) {
            MedicalReportInfo medicalReportInfo = patientMapper.getMedicalReportInfo(medicalReportListResult.getReportId());
            String doctorName = patientMapper.selectRealNameById(medicalReportInfo.getVerifyDoctorId());
            String reporterName = patientMapper.selectRealNameById(medicalReportInfo.getReporterId());
            medicalReportInfo.setVerifyDoctorName(doctorName);
            medicalReportInfo.setReporterName(reporterName);

            medicalReportListResult.setMedicalReportInfo(medicalReportInfo);
        }
        return reportList;
    }

    @Override
    public List<EmpowerResult> getEmpowerList(String username) {
        String patientId = patientMapper.selectPatientIdByUsername(username);
        List<EmpowerResult> resultList = patientMapper.getEmpowerList(patientId);
        for (EmpowerResult object : resultList) {
            object.setDoctorName(patientMapper.selectRealNameById(object.getDoctorId()));
            object.setHospitalName(patientMapper.selectHospitalNameByDoctorId(object.getDoctorId()));
        }
        return resultList;
    }

    @Override
    public void updateEmpowerStatus(EmpowerParam empowerParam) {
            patientMapper.updateEmpowerStatus(empowerParam);
    }

    @Override
    public List<EmpowerResult> getCheckupList(String username) {
        String patientId = patientMapper.selectPatientIdByUsername(username);
        List<EmpowerResult> resultList = patientMapper.getReportEmpowerList(patientId);
        for (EmpowerResult object : resultList) {
            object.setDoctorName(patientMapper.selectRealNameById(object.getDoctorId()));
            object.setHospitalName(patientMapper.selectHospitalNameByDoctorId(object.getDoctorId()));
        }
        return resultList;
    }

    @Override
    public void updateCheckupStatus(EmpowerParam empowerParam) {
            patientMapper.updateCheckupStatus(empowerParam);
    }

    @Override
    public List<EmrMedOrders> getMyPrescription(String id) {
        List<EmrMedOrders> resultList = patientMapper.getMyPrescription(id);
        return resultList;
    }
}
